package com.cug.nageoffer.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.cug.nageoffer.common.mapper.EnterpriseMapper;
import com.cug.nageoffer.common.mapper.JobMapper;
import com.cug.nageoffer.common.service.EnterpriseService;
import com.cug.nageoffer.model.entity.Enterprise;
import com.cug.nageoffer.model.vo.EnterpriseDetailVo;
import com.cug.nageoffer.model.vo.EnterpriseVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 根据行业类型查询公司
     * @param type
     * @return
     */

    @Override
    public PageInfo<EnterpriseVo> getEnterpriseInfoByType(String type, Integer pageNum, Integer pageSize) {
        String cacheKey = "enterprise:" + type + ":" + pageNum + ":" + pageSize;
        // 查询redis
        String cacheData = redisTemplate.opsForValue().get(cacheKey);
        PageInfo<EnterpriseVo> pageInfoCache = (PageInfo<EnterpriseVo>)JSON.parseObject(cacheData,PageInfo.class);
        if(pageInfoCache != null && !pageInfoCache.getList().isEmpty()){
            // 如果缓存数据存在，则直接返回
            pageInfoCache.setPageNum(pageNum);
            pageInfoCache.setPageSize(pageSize);
            return pageInfoCache;
        }else {
            PageHelper.startPage(pageNum, pageSize);
            List<EnterpriseVo> list = enterpriseMapper.selectByType(type);
            PageInfo<EnterpriseVo> pageInfo = new PageInfo<>(list);
            Random random = new Random();
            int timeOut = random.nextInt(1,5);
            // 将查询结果存储到Redis缓存中
            redisTemplate.opsForValue().set(cacheKey, JSON.toJSONString(pageInfo), timeOut , TimeUnit.HOURS);
            return pageInfo;
        }

    }

    /**
     * 根据id查询公司
     * @param id
     * @return
     */
    @Override
    public EnterpriseDetailVo getById(Long id) {
        // 查询公司信息
        Enterprise enterprise = enterpriseMapper.selectById(id);
        // 查询该公司hr数量

        // 查询该公司岗位数量
        Integer JobCount = jobMapper.countJobByEnterpriseId(enterprise.getId());

        // 构造返回数据
        EnterpriseDetailVo enterpriseDetailVo = new EnterpriseDetailVo();
        BeanUtils.copyProperties(enterprise,enterpriseDetailVo);
        enterpriseDetailVo.setHrCount(1);
        enterpriseDetailVo.setJobCount(JobCount);
        return enterpriseDetailVo;
    }

    /**
     * 查询热点企业
     * @return
     */
    @Override
    @Cacheable(value = "enterprise" , key = "'hot'" )
    public List<EnterpriseVo> getHotEnterprise() {
        return enterpriseMapper.selectHotEnterprise(12);
    }
}
